<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      :title="$t('msg.setting.title')"
      width="500px"
      :before-close="handleClose"
      @opened="handleOpen">
      <el-form ref="formRef" :model="form" size="small" label-width="100px">
        <el-form-item :label="$t('msg.setting.language')">
          <el-select v-model="form.language" @change="handleLanguageChange">
            <el-option :label="$t('msg.setting.zh')" value="zh"></el-option>
            <el-option :label="$t('msg.setting.en')" value="en"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('msg.setting.zoom')">
          <el-select v-model="form.zoom" @change="handleZoomChange">
            <el-option :label="$t('msg.setting.followSystem')" :value="0"></el-option>
            <el-option label="100%" :value="1"></el-option>
            <el-option label="125%" :value="1.25"></el-option>
            <el-option label="150%" :value="1.5"></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item :label="$t('msg.setting.species')">-->
<!--          <theme-picker/>-->
<!--        </el-form-item>-->
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, defineProps, defineEmits, computed } from 'vue'
import { useStore } from 'vuex'
import { useI18n } from 'vue-i18n'
import { setScale } from '@/utils/screen-scale'
// import ThemePicker from '../ThemePicker/index.vue'

// ---------------------store-------------------
const store = useStore()

// ---------------------props-------------------
const props = defineProps({
  visible: Boolean // 可见性
})

// emit
const emit = defineEmits(['update:visible', 'dialogCloseEvent'])

// ---------------------el-Dialog组件需要一些参数-------------------
// 组件可见性
const dialogVisible = computed({
  get: () => props.visible,
  set: (v) => {
    emit('update:visible', v)
  }
})
// 关闭弹窗事件
const handleClose = () => {
  dialogVisible.value = false
  emit('dialogCloseEvent')
}

// ---------------------设置内容的form-------------------
const formInit = {
  language: store.getters.language,
  zoom: store.getters.windowScale
}
const form = reactive(formInit)
// 页面打开，初始化
const handleOpen = () => {
  form.language = store.getters.language
}

// ---------------------设置语言-------------------
const { locale } = useI18n()
// 语言改变
const handleLanguageChange = (lang) => {
  if (lang) {
    store.dispatch('app/setLanguage', lang)
    locale.value = lang
  }
}

const handleZoomChange = zoom => {
  setScale(zoom)
}

</script>

<style scoped lang="scss">
.input-width{
  width: 300px;
}
</style>
